Step 4 - Use keyboard keys to scroll

In this step of the tutorial you learn how to use keyboard keys to scroll in those application components which support scrolling.

Set keyboard keys to scroll the list box in the Car application screen

In this section you use the trySetFocus() scripting function to set focus to the Car Grid List Box node. When you set focus to that node, you can scroll the list items in the Car application screen.

To set keyboard keys to scroll the list box in the Car application screen:

  1. In the Project > RootPage select the Car node and in the Node Components add to the Page Activated trigger an Execute Script action.
    You use this trigger to execute a script which sets focus to the Car Grid List Box when the Page node Car is activated. When a Grid List Box node has focus, you can use:
  2. Create a script and in the Script Editor use this script:
    // Get the Car Grid List Box node.
    var carGridListBox = node.lookupNode('#Car Grid List Box');
    // Set focus to the Car Grid List Box node.
    carGridListBox.trySetFocus();

    You use this script to set focus to the Car Grid List Box node when the Page node Car is activated. When you use the (Left Arrow) or (Right Arrow) keys to navigate to the Car application screen, you can use the (Up Arrow) and (Down Arrow) keys to scroll the selection of list items in the Car Grid List Box.

  3. In the Project > RootPage > Car select the Car Grid List Box node and in the Node Components add the Key Down trigger.
    You use this trigger to execute the NavigateRight.js script which you created in the first step of this tutorial. Because the Car Grid List Box node has focus when the Page node Car is active, the (Right Arrow) and (Left Arrow) keys on the keyboard receive input only when another Page is activated.
  4. In the Trigger Settings for that trigger click Add condition, and in the Trigger Condition Editor set:

    In the Trigger Condition Editor and the Trigger Settings Editor windows click Save.

    You set the trigger to be set off when the user presses the (Right Arrow) key.

  5. Add to the Key Down trigger an Execute Script action and use the NavigateRight.js script.
    You set the application to execute the NavigateRight.js script when the trigger is set off.
  6. Add another trigger for the Key Down, in the Execute Script action use the NavigateLeft.js script, and set it to be set off when the user presses the (Left Arrow) key.

In the Preview when you use the (Right Arrow) or (Left Arrow) keys to navigate to the Page node Car, use:

Set keyboard keys to scroll the list box in the Navigation screen

In this section you use the trySetFocus() scripting function to set focus to the Navigation Grid List Box node. When you set focus to that node, you can scroll the list items on the Navigation application screen.

To set keyboard keys to scroll the list on the Navigation application screen:

  1. In the Project > RootPage select the Page Host node Navigation node and in the Node Components add to the Page Activated trigger an Execute Script action.
    You use this trigger to execute a script which sets focus to the Navigation Grid List Box when the Page Host node Navigation is activated.
  2. Create a script and in the Script Editor use this script:
    // When the Page node Navigation is activated, set focus to the Grid List Box 2D node to enable scrolling.
    var navigationGridListBox = node.lookupNode('#Navigation Grid List Box');
    navigationGridListBox.trySetFocus();
    

    You use this script to set focus to the Navigation Grid List Box node when the Page Host node Navigation is activated. When you use the (Left Arrow) or (Right Arrow) keys to navigate to the Navigation application screen, you can use the (Up Arrow) and (Down Arrow) keys to scroll the selection of list items in the Navigation Grid List Box.

  3. In the Project select the Car Grid List Box node and in the Node Components copy the Key Down trigger.
  4. In the Project > RootPage > Navigation > Grid Layout 2D select the Navigation Grid List Box node and in the Node Components paste the Key Down trigger.

In the Preview when you use the (Right Arrow) or (Left Arrow) keys to navigate to the Page node Car, use the (Up Arrow) and (Down Arrow) keys to scroll the selection of list items. The Home and Page Up keys select the first list item and the End and Page Down keys select the last list item.

Set keyboard keys to scroll the Map application screen

In this section you use the trySetFocus() scripting function to set focus to the Scroll View 2D node. When you set focus to that node, you can scroll the map on the Map application screen using keyboard keys.

To set keyboard keys to scroll the Map application screen:

  1. In the Project > RootPage > Navigation select the Map node and in the Node Components add to the Page Activated trigger an Execute Script action.
    You use this trigger to execute a script which sets focus to the Scroll View 2D node when the Page node Map is activated. When a Scroll View node has focus, you can use the arrow keys on the keyboard to scroll the contents of that node.
  2. Create a script and in the Script Editor use this script:
    // Get the Scroll View 2D node.
    var scrollView = node.lookupNode('#Scroll View 2D');
    // Set focus to the Scroll View 2D node.
    scrollView.trySetFocus();

    You use this script to set focus to the Scroll View 2D node when the Page node Map is activated. When you navigate to the Map application screen, you can use the arrow keys on the keyboard to scroll the map.

Set a key to open the Map application screen

In this section you set the Map application screen window to open when the user presses the Enter key on the keyboard.

To set a key to open the Map application screen:

  1. In the Project > RootPage > Navigation > Grid Layout 2D select the Navigation Grid List Box node and in the Node Components click Add new trigger for Key Down.
  2. In the Trigger Settings Editor for the Key Down trigger you created in the previous step add these conditions:
  3. In the Project > Navigation > Grid Layout 2D select the OpenMap node, in the Node Components copy and paste each action from the Button: Click trigger to the Key Down trigger you added for the Navigation Grid List Box node in the previous step.

Set a key to close the Map application screen

In this section you set the Map window to close when the user presses the E key.

To set a key to close the Map application screen:

  1. In the Project > RootPage > Navigation > Map select the Scroll View 2D node and in the Node Components add the Key Down trigger.
    You use this trigger to close the Map when the user presses a key on the keyboard.
  2. Add to the Key Down trigger a condition which sets the trigger off when the user presses the E key.
  3. Add to the Key Down trigger the actions you use to close the Map page:

    In the Preview use the E key to close the Map application screen.

Finalize the keyboard navigation

In this section you finalize the keyboard navigation to make sure that the key input works together with the pointer input. Because you set the focus using Key Down triggers, which the user sets off by pressing a key on the keyboard, using the pointer does not set the focus in the correct place. If you use the keyboard to navigate to a Page node which gets focus, and then use the pointer to close that application screen, the focus remains on that Page node.

To finalize the keyboard navigation:

  1. In the Project > RootPage select the Page Host node Home, in the Node Components add to the Page Activated trigger an Execute Script action, and use the SetFocusToScreen.js script.
    You set focus to the Home node when that node is activated. When you use the keyboard keys to navigate in a part of the application which has focus and use the pointer to navigate to the Page Host node Home, the Home node gets the focus.
  2. Repeat the previous step for the Page node Media.
  3. In the Project > Prefabs > Air Condition Navigation select the Button 2D node Close, in the Node Components add to the Button: Click trigger an Execute Script action, and use the SetFocusToScreen.js script.
    You set the Button: Click trigger in the Close button to set off a script which sets focus to the Screen. When you navigate in the AirCondition window and use the pointer to close that window, the Home node gets the focus.
  4. In the Project > RootPage > Navigation > Map select the Button 2D node CloseMap and repeat the previous step for that node.
    You set the Button: Click trigger in the CloseMap button to execute a script which sets focus to the Screen.

Organize the script files in your Kanzi Studio project

In this section you rename the scripts you created in this step of the tutorial and create folders for those scripts.

To organize the script files in your Kanzi Studio project:

  1. In the Library > Resource Files > Scripts rename:
  2. In the Library > Resource Files > Scripts:

In the Preview you can now use both the keyboard keys you have defined in this tutorial and your pointer to navigate the application.

Use these keyboard keys to navigate the application:


< PREVIOUS STEP

What's next?

In this tutorial you learned how to add keyboard input to an application using the Key Down trigger and JavaScript scripts. Now you can:

See also

Tutorial: Create application flow with Page nodes

Using the Page and Page Host nodes

Using triggers

Keyboard input codes reference

Using scripts

Scripting reference